package cn.suimg.bilibili.service;

import cn.suimg.bilibili.api.BarrageConsumer;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 默认弹幕消费实现
 */
public class DefaultBarrageConsumerImpl implements BarrageConsumer {

    /**
     * Logger
     */
    private static final Logger logger = LoggerFactory.getLogger(DefaultBarrageConsumerImpl.class);

    @Override
    public void onNoticeMsg(JSONObject json) {
        onSysMsg(json);
    }

    @Override
    public void onSysMsg(JSONObject json) {
        logger.info(json.getString("msg_common").replaceAll("<%","「").replaceAll("%>","」"));
    }

    @Override
    public void onWelcome(JSONObject json) {
        JSONObject welcome = json.getJSONObject("data");
        String isAdmin = welcome.getBoolean("is_admin") ? "[房管]" : "";
        String vipLevel = welcome.containsKey("vip") ? "老爷" : "年费老爷";
        logger.info("{}({}){}{}进入直播间",welcome.getString("uname"),welcome.getInteger("uid"),isAdmin,vipLevel);
    }

    @Override
    public void onDanmuMsg(JSONObject json) {
        JSONArray info = json.getJSONArray("info");
        JSONArray userInfo = info.getJSONArray(2);
        logger.info("{}({}):{}",userInfo.getString(1),userInfo.getInteger(0),info.getString(1));
    }

    @Override
    public void onReltimeUpdate(JSONObject json) {
        JSONObject fans = json.getJSONObject("data");
        logger.info("当前粉丝数:{}",fans.getInteger("fans"));
    }

    @Override
    public void onSendGift(JSONObject json) {
        JSONObject gift = json.getJSONObject("data");
        logger.info("{}({})赠送「{}」x{}",gift.getString("uname"),gift.getInteger("uid"),gift.getString("giftName"),gift.getInteger("num"));
    }

    @Override
    public void onRoomRank(JSONObject json) {
        JSONObject rank = json.getJSONObject("data");
        logger.info("直播排行:{} -> {}",rank.getInteger("roomid"),rank.getString("rank_desc"));
    }
}
